package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.common.utils.JwtHelper;
import com.atguigu.spzx.common.utils.MD5;
import com.atguigu.spzx.common.constant.SysUserStatusEnum;
import com.atguigu.spzx.common.entity.SysUser;
import com.atguigu.spzx.manager.manager.PermissionManager;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.mapper.SysUserMapper;
import com.atguigu.spzx.manager.service.IndexService;
import com.atguigu.spzx.common.result.ResultCodeEnum;
import com.atguigu.spzx.manager.vo.index.RouterVo;
import com.atguigu.spzx.manager.vo.SysLoginReqVo;
import com.atguigu.spzx.manager.vo.SysLoginRespVo;
import com.atguigu.spzx.manager.vo.sysUser.SysUserInfoRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * IndexService
 * </p>
 *
 * @author flyduck
 * @since 2023/10/26
 */
@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private PermissionManager permissionService;

    /**
     * 登录
     * @param reqVo
     * @return
     */
    @Override
    public SysLoginRespVo login(SysLoginReqVo reqVo) {
        SysLoginRespVo respVo = new SysLoginRespVo();
        //根据username查询用户，判断用户是否存在
        SysUser sysUser = sysUserMapper.getUserByUsername(reqVo.getUsername());
        if (sysUser == null) {
            throw new GuiguException(ResultCodeEnum.ACCOUNT_ERROR);
        }

        //判断密码是否一致
        if (!sysUser.getPassword().equals(MD5.encrypt(reqVo.getPassword()))) {
            throw new GuiguException(ResultCodeEnum.PASSWORD_ERROR);
        }

        //判断是否可用
        if (SysUserStatusEnum.STOP.getCode().equals(sysUser.getStatus())) {
            throw new GuiguException(ResultCodeEnum.ACCOUNT_STOP);
        }

        //生成token
        String token = JwtHelper.createToken(sysUser.getId(), sysUser.getUsername());
        respVo.setToken(token);
        return respVo;
    }

    /**
     * 查询用户信息
     * @param token
     * @return
     */
    @Override
    public SysUserInfoRespVo getUserInfo(String token) {
        SysUserInfoRespVo respVo = new SysUserInfoRespVo();

        //从token获取用户名
        String username = JwtHelper.getUsername(token);

        //根据username获取信息（基本信息、菜单权限、按钮权限）
        //基本信息
        SysUser sysUser = sysUserMapper.getUserByUsername(username);
        //根据userId获取菜单权限
        List<RouterVo> routers = permissionService.getRouterVoListByUserId(sysUser.getId());
        //根据userId获取按钮权限
        List<String> buttons = permissionService.getUserPermsListByUserId(sysUser.getId());

        respVo.setName(username);
        respVo.setAvatar(sysUser.getHeadUrl());
        respVo.setRoles(Arrays.asList("admin"));
        respVo.setRouters(routers);
        respVo.setButtons(buttons);
        return respVo;
    }


}
